package com.z.redis.channel.epoll;

import com.z.redis.channel.LocalChannelOption;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollServerSocketChannel;

import java.util.Objects;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

public class EpollChannelOption implements LocalChannelOption {
    private final EpollEventLoopGroup boss;
    private final EpollEventLoopGroup workers;

    public EpollChannelOption(EpollEventLoopGroup boss, EpollEventLoopGroup workers) {
        this.boss = Objects.requireNonNull(boss, "boss group cannot be null");
        this.workers = Objects.requireNonNull(workers, "workers group cannot be null");
    }
    public EpollChannelOption(){
        this.boss = new EpollEventLoopGroup(4, new ThreadFactory() {
            private AtomicInteger index = new AtomicInteger(0);

            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r,"Server_boss_" + index.getAndIncrement());
            }
        });
        this.workers = new EpollEventLoopGroup(8, new ThreadFactory() {
            private AtomicInteger index = new AtomicInteger(0);

            @Override
            public Thread newThread(Runnable r){
                return new Thread(r,"Server_workers_" + index.getAndIncrement());
            }
        });
    }
    @Override
    public EpollEventLoopGroup boss() { return this.boss; }
    @Override
    public EpollEventLoopGroup workers() { return this.workers; }
    @Override
    public Class getChannelClass() {
        return EpollServerSocketChannel.class;
    }
}
